package com.gec.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gec.entity.Depts;
import com.gec.service.DeptsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author GOF
 * @since 2021-11-04
 */
@Controller
@RequestMapping("/depts")
public class DeptsController {

    @Autowired
    private DeptsService deptsService;

    @RequestMapping("/list")
    public String showList(Model model,String findName){
        QueryWrapper<Depts> ew = new QueryWrapper<>();

        ew.lambda().like(findName!=null,Depts::getName,findName);

        List<Depts> depts = deptsService.list(ew);
        model.addAttribute("depts",depts);
        return "dept_list";
    }

    @GetMapping("/edit")
    public String edit(Integer id,Model model){
        Depts dept = deptsService.getById(id);
        model.addAttribute("dept",dept);
        return "dept_edit";
    }

    @PostMapping("/edit")
    public String edit(Depts depts){
        deptsService.saveOrUpdate(depts);
        return "redirect:list";
    }
}
